<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/27
 * Time: 下午3:24
 */

namespace App\Busi\Traits;


use App\Models\News;
use App\Models\SecQualifyCategory;
use App\Models\SupQualifyCategory;

Trait NewsIndexTrait
{
    public function getNewsIndexList(){
        $where = [
            ['flag',"=",1]
        ];
        $fields = ['id',"supcategory_id","subcategory_id","title","poster","desc","created_at as create_time","view_num"];
        $data = News::where($where)->select($fields)->orderBy("view_num","desc")->orderBy("show_order","desc")->get();
        $category_list = [];
        $news_list = [];
        $category_id  = [];
        $sup_category_list = [];
        foreach ($data as $item){


            $news_item = [];
            $news_item["id"] = $item["id"];
            $news_item["title"] = $item["title"];
            $news_item["desc"] = $item["desc"];
            $news_item["view_num"] = $item["view_num"];
            $news_item["poster"] = config('admin.upload.host').$item['poster'];
            $news_item["create_time"] = substr($item['create_time'],0,10);
            array_push($category_id,$item["subcategory_id"]);
            array_push($news_list,$news_item);
        }
        $unique_category_id = array_unique ( $category_id );
        foreach ($unique_category_id as $id){
            $category_item  = [];
            $category_item["id"] = $id;
            $category_item["category_name"] = $this->getSubCategoryNameByID($id);
            array_push($category_list,$category_item);
        }

        $ret = [
            "hot_categoryList"=>$category_list,
            "hot_newsList"=>$news_list,
            "footer_list"=>$this->getSupCategoryNewsList(),
        ];
        return $ret;

    }
    function getSupCategoryNewsList(){

        $where = [
            ['flag',"=",1]
        ];
        $fields = ['id',"supcategory_id","subcategory_id","title","poster","desc"];
        $data = News::where($where)->select($fields)->limit(1)->orderBy("view_num","desc")->orderBy("show_order","asc")->get();

        $category_list = [];

        foreach ($data as $item){
            $sup_category_id = $item['supcategory_id'];
            $category_list["name"] = $this->getSupCategoryNameByID($item['supcategory_id']);
            $category_list["news_lists"] = $this->getNewsBySupCategoryId($sup_category_id);
            $category_list["sub_categorys"] = $this->getChildCategorysByPid($sup_category_id);


        }
        return $category_list;





    }
    function getSubCategoryNameByID($id){
        $where = [
            ['id','=',$id]
        ];
        $fields = ['id','category_name'];
        $data = SecQualifyCategory::where($where)->select($fields)->first();
        if($data){
            $data = $data->toArray();
            return $data['category_name'];

        }else{
            return "";
        }

    }
    function getSupCategoryNameByID($id){
        $where = [
            ['id','=',$id]
        ];
        $fields = ['id','category_name'];
        $data = SupQualifyCategory::where($where)->select($fields)->first();
        $data = $data->toArray();
        if($data){
            return $data['category_name'];

        }else{
            return "";
        }

    }
    /**
     * 根据父级id获取子分类
     * @param $parent_cate_id
     */
    public function getChildCategorysByPid($parent_cate_id){
        $where = [
            ["parent_cate_id","=",$parent_cate_id],
            ["flag","=",1]
        ];
        $fileds = ["id","category_name"];
        $sub_category_list = [];
        $data = SecQualifyCategory::where($where)->select($fileds)->orderBy("show_order","asc")->get();
        foreach ($data as $item){
            $cate_item = [];
            $cate_item["id"] = $item["id"];
            $cate_item["category_name"] = $item["category_name"];
            array_push($sub_category_list,$cate_item);
        }
        return $sub_category_list;


    }
    public function getNewsBySupCategoryId($supcateid){
        $where = [
            ["supcategory_id","=",$supcateid],
            ['flag',"=",1]
        ];
        $fields = ['id',"supcategory_id","subcategory_id","title","poster","desc","created_at"];
        $data = News::where($where)->select($fields)->orderBy("view_num","desc")->orderBy("show_order","desc")->get();
        $news_list = [];
        foreach ($data as $item){
            $news_item = [];
            $news_item["id"] = $item["id"];
            $news_item["title"] = $item["title"];
            $news_item["desc"] = $item["desc"];
            $news_item["poster"] = config('admin.upload.host').$item['poster'];
            $news_item["create_time"] = substr($item['created_at'],0,10);
            array_push($news_list,$news_item);
        }
        return $news_list;
    }

    public function getNewsList($page,$subcategory_id){
        $pageSize = 10;
        if($subcategory_id){
            $where = [
                ["subcategory_id","=",$subcategory_id],
                ["flag","=",1]
            ];
        }else{
            $where = [
                
                ["flag","=",1]
            ];
        }

        $fields = ['id',"supcategory_id","subcategory_id","title","poster","desc","view_num","key_words","created_at"];
        $data = News::where($where)->select($fields)->orderBy('id', 'asc')->paginate($pageSize);
        $total = $data->total();
        $pageNum =  ceil($total/$pageSize);

        $newsList = [];
        foreach ($data as $item){
            $news_item = [];
            $news_item["id"] = $item["id"];
            $news_item["title"] = $item["title"];
            $news_item["desc"] = $item["desc"];
            $news_item["poster"] = config('admin.upload.host').$item['poster'];
            $news_item["view_num"] = $item["view_num"];
            $news_item["key_words"] = $item["key_words"];
            $news_item["create_time"] = substr($item["created_at"],0,20);
            array_push($newsList,$news_item);

        }
        $ret = [
            "totalPage"=>$pageNum,
            "currentPage"=>$data->currentPage(),
            "list"=>$newsList,
        ];

        return $ret;
    }
    public function getNewsDetail($id){
        $where = [
            ["id","=",$id],
            ['flag',"=",1]
        ];

        $fields = ["title","view_num","key_words","created_at as create_time","content"];
        $detail = News::where($where)->select($fields)->first();
        $last_id = News::where('id', '<', $id)->max('id');
        $next_id = News::where('id', '>', $id)->min('id');
        $last_news = News::where('id', '=', $last_id)->select(["id","title"])->first();

        $next_news = News::where('id', '=', $next_id)->select(["id","title"])->first();
        if($last_news ==null){
            $last_news["id"] = 0;
            $last_news["title"] = "没有上一条了";

        }
        if($next_news ==null){
            $next_news["id"] = 0;
            $next_news["title"] = "没有下一条了";

        }
        $recommend = News::where("flag","=",1)->select(["id","title"])->orderBy(\DB::raw('RAND()'))->take(2)->get();
        $ret = [
            "detail"=>$detail,
            "last_news"=>$last_news,
            "next_news"=>$next_news,
            "recommend"=>$recommend
        ];
        return $ret;
    }

}